﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Passion3DProject.src.About_the_Camera;

namespace Passion3DProject.src.Stuffs.About_Stages
{
    class World : Stage
    {
        SkySphere ceu;

        GameObject ground;
        Player player;
        ThirdPersonCamera camera;
        GameObjectManager objManager;
        GameObject[] wall = new GameObject[500];
        GameObject[] item = new GameObject[20];
        Random random = new Random();        
        
        GraphicsDevice GD;

        public World(ContentManager content, GraphicsDevice GD)
            :base(content)
        {
            this.GD = GD;

            ceu = new SkySphere(Content, GD, Content.Load<TextureCube>("Black_sky"));
        }

        protected override void LoadContent()
        {            
            objManager = new GameObjectManager();
            ground = new GameObject(Content, "grounds");
            player = new Player(Content, "caixa");
            camera = new ThirdPersonCamera();
            
            for (int i = 0; i < wall.Length; i++)
            {                
                wall[i] = new GameObject(Content, "piramide");
                wall[i].ScaleX = wall[i].ScaleY = wall[i].ScaleZ = 1f;
                wall[i].Position = new Vector3(random.Next(-750, 750), 0, random.Next(-750, 750));
                
                //percorrer a lista objManager (mas testando se eu tenho apenas meu objeot piramide) e testar se a posicao do objeot sendo criado coincide com alguma na lista, senao sorteia de novo
                //WHILE ou um DO WHILE

                objManager.ObjectList.Add(wall[i]);                            
            }
                        
            for (int i = 0; i < item.Length; i++)
            {
                item[i] = new GameObject(Content, "objetos");
                item[i].ScaleX = item[i].ScaleY = item[i].ScaleZ
                    = 0.05f;
                objManager.ObjectList.Add(item[i]);
                item[i].Position = new Vector3(random.Next(-750, 750), 0, random.Next(-750, 750));
            }

            objManager.ObjectList.Add(ground);
            objManager.PlayerList.Add(player);
            ground.ScaleX = ground.ScaleY = ground.ScaleZ = 10;
            player.Position = new Vector3(10, 0.50009f, 50);
            player.LastPosition = player.Position;

        }

        public override void Update(GameTime gameTime)
        {
            camera.Update(player.PlayerDirection, player.Position);
            objManager.Update(gameTime);
        }

        public override void Draw()
        {
            ceu.Draw(camera.viewMatrix, camera.ProjectionCamMatrix, camera.Position);
            objManager.Draw(camera);            
        }
    }
}
